package com.google.devrel.example.android.protips.adapter;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.devrel.example.android.protips.R;

public class CachedAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private List<String> data;
	private int viewId;
	
	public CachedAdapter(LayoutInflater inflater, List<String> data, int view) {
		this.inflater = inflater;
		this.data = data;
		this.viewId = view;
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return (long)position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getViewSlow(position, convertView, parent);
//		return getViewBetter(position, convertView, parent);
//		return getViewBest(position, convertView, parent);
	}
	
	public View getViewSlow(int position, View convertView, ViewGroup parent) {
		
		View item = inflater.inflate(viewId, null);
		((TextView)item.findViewById(R.id.title)).setText(data.get(position));
		((TextView)item.findViewById(R.id.text1)).setText("-");
		((TextView)item.findViewById(R.id.text2)).setText("_");
		((TextView)item.findViewById(R.id.text3)).setText("-");
		((TextView)item.findViewById(R.id.text4)).setText("!");
		
		return item;
	}
	
	public View getViewBetter(int position, View convertView, ViewGroup parent) {

		if (convertView==null) {
			convertView = inflater.inflate(viewId, null);
		}
		
		((TextView)convertView.findViewById(R.id.title)).setText(data.get(position));
		((TextView)convertView.findViewById(R.id.text1)).setText("-");
		((TextView)convertView.findViewById(R.id.text2)).setText("_");
		((TextView)convertView.findViewById(R.id.text3)).setText("-");
		((TextView)convertView.findViewById(R.id.text4)).setText("!");
		
		return convertView;
	}

	public View getViewBest(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		
		if (convertView==null) {
			convertView = inflater.inflate(viewId, null);
			
			holder = new ViewHolder();
			holder.view = (TextView)convertView.findViewById(R.id.title);
			holder.text1 = (TextView)convertView.findViewById(R.id.text1);
			holder.text2 = (TextView)convertView.findViewById(R.id.text2);
			holder.text3 = (TextView)convertView.findViewById(R.id.text3);
			holder.text4 = (TextView)convertView.findViewById(R.id.text4);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.view.setText(data.get(position));
		holder.text1.setText("-");
		holder.text2.setText("_");
		holder.text3.setText("-");
		holder.text4.setText("!");
		
		return convertView;
	}
	
	static class ViewHolder {
        TextView view;
        TextView text1;
        TextView text2;
        TextView text3;
        TextView text4;
    }

}
